#include "RenderSystem.h"
#include "HardwareResourceManager.h"
#include "D3D10RenderDevice.h"
#include "Utility/Utility.h"
#include "MeshBuffer.h"
#include "LightPass.h"

CRenderSystem::CRenderSystem(CD3D10RenderDevice* pRenderDev)
	: m_pRenderDev(pRenderDev)
{

}

CRenderSystem::~CRenderSystem()
{
	UnInit();
}

void CRenderSystem::Init()
{
	m_pLightPass = new CLightPass;
	m_pLightPass->Init();
}

void CRenderSystem::UnInit()
{
	if (m_pLightPass)
	{
		m_pLightPass->UnInit();
		SAFE_DELETE(m_pLightPass);
	}

	m_pRenderDev = NULL;
}

void CRenderSystem::BeginRender()
{
	m_pRenderDev->ClearScene();
}

void CRenderSystem::EndRender()
{
	for (UINT32 i = 0; i < m_vecRenderObjs.size(); i++)
	{
		SAFE_DELETE(m_vecRenderObjs[i]);
	}
	m_vecRenderObjs.clear();
}

void CRenderSystem::Render( const CRenderObject& renderObj )
{
	switch(renderObj.eType)
	{
	case ERENDER_OBJ_MESH:
		{
			CMeshRenderObject *pMeshRenderObj = new CMeshRenderObject;
			*pMeshRenderObj = dynamic_cast<const CMeshRenderObject&>(renderObj);
			m_vecRenderObjs.push_back(pMeshRenderObj);
		}
	}
}

void CRenderSystem::DrawAll()
{
	m_pLightPass->BeginRender();
	m_pLightPass->Render(&m_vecRenderObjs);
	m_pLightPass->EndRender();
}


void CRenderSystem::DrawMesh( CMeshBuffer* pMesh )
{
	m_pRenderDev->DrawIndexedPrimitiveList(
		pMesh->m_pVertexBuffer,  pMesh->m_pIndexBuffer, 
		pMesh->m_dwVertexOffset, pMesh->m_dwIndexOffset);
}
